package com.swh.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/12/29 11:55
 * @Version: 1.0
 */
//连接MvcConfig拦截器
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");

        if(loginUser==null){
            //用户还没有登录
            response.sendRedirect(request.getContextPath()+"/html/notlogin.html");
//            System.out.println(request.getContextPath()+"/notlogin.html");
//            System.out.println("getContextPath:"+request.getContextPath());
            return false;
        }
        return true;
    }
}
